package com.ydzt.fanka.server.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.log4j.Logger;


public class JdbcUtils {
	@SuppressWarnings("unused")
	private static JdbcUtils jdbc;
	
	static Logger logger = Logger.getLogger(JdbcUtils.class.getName());
	
	private JdbcUtils(){
		
	}
	
	static{
		try {
			Class.forName(Constant.DATABASE_TYPE_ORACLE);
		} catch (ClassNotFoundException e) {
			logger.error("注册JDBC驱动出错" + e.getMessage());
		}
	}
	
	public static Connection getConnectionForOracle() {
		Connection conn = null;
		//System.out.println(Constant.ORACLE_URL + " " + Constant.USERNAME + " " + Constant.PASSWORD);
		try {
			conn = DriverManager.getConnection(Constant.ORACLE_URL,Constant.USERNAME,Constant.PASSWORD);
		} catch (SQLException e) {
			logger.error("连接数据库失败:" + e.getMessage());
		}
		return conn;
	}
	
	public static void free(ResultSet rs, Statement stmt, Connection conn){
		try {
			if(rs != null){
				rs.close();
			}
		} catch (SQLException e) {
			logger.error("释放JDBC资源出错:" + e.getMessage());
		} finally{
			try {
				if(stmt != null){
					stmt.close();
				}
			} catch (SQLException e) {
				logger.error("释放JDBC资源出错:" + e.getMessage());
			} finally{
				/*
				try {
					if(conn != null){
						conn.close();
					}
				} catch (SQLException e) {

					e.printStackTrace();
				}
				*/
			}
		}
	}
	
	public static void freeAll(ResultSet rs, Statement stmt, Connection conn){
		try {
			if(rs != null){
				rs.close();
			}
		} catch (SQLException e) {
			logger.error("释放JDBC资源出错:" + e.getMessage());
		} finally{
			try {
				if(stmt != null){
					stmt.close();
				}
			} catch (SQLException e) {
				logger.error("释放JDBC资源出错:" + e.getMessage());
			} finally{
				
				try {
					if(conn != null){
						conn.close();
					}
				} catch (SQLException e) {
					logger.error("释放JDBC资源出错:" + e.getMessage());
				}
				
			}
		}
	}
}
